#Include 'Protheus.ch'

/*/{Protheus.doc} DTCOMA19
(long_description)
@author Microsiga
@since 19/12/2014
@version 1.0
@return ${return}, ${return_description}
@example
(examples)
@see (links_or_references)
/*/
User Function DTCOMA19(aRecnoCN9)
Local aArea		:= GetArea()
Local cQuery		:= ""
Local cAliasTmp	:= GetNextAlias()
Local nX 			:= 0
Local nPrazo		:= 0
Local nRecnoCNB	:= 0
Local nRecnoCN9	:= 0

CO1->(dbSetOrder(1)) //CO1_FILIAL+CO1_CODEDT+CO1_NUMPRO

FKCOMMIT() //garantir a gravacao durante o transacao
For nX := 1 To Len(aRecnoCN9)
	nRecnoCN9	:= (aRecnoCN9[nX])
	CN9->(DbGoto(nRecnoCN9))
	If CO1->(dbSeek(xFilial("CO1")+CN9->(CN9_CODED+CN9_NUMPR)))
		nPrazo	:= CO1->CO1_XPRZEN
		If nPrazo <> 0
			cQuery := "SELECT CNB.R_E_C_N_O_ CNBRECNO FROM " +RetSqlName("CNB") +" CNB "
			cQuery += "WHERE CNB.D_E_L_E_T_ = ' ' AND "
			If CNB->(FieldPos("CNB_FILORI")) == 0
				cQuery += "	CNB.CNB_FILIAL = '" +CN9->CN9_FILIAL +"' AND "
			Else
				cQuery += "	CNB.CNB_FILORI = '" +CN9->CN9_FILORI +"' AND "
			EndIf
			cQuery += "	CNB.CNB_CONTRA = '"+CN9->CN9_NUMERO +"' AND "
			cQuery += "	CNB.CNB_REVISA = '"+CN9->CN9_REVATU +"'"
			cQuery := ChangeQuery(cQuery)
			dbUseArea(.T.,"TOPCONN",TcGenQry(,,cQuery),cAliasTmp)

			While (cAliasTmp)->(!Eof())
				nRecnoCNB	:= (cAliasTmp)->CNBRECNO
				CNB->(DbGoto(nRecnoCNB))
				If (nPrazo != CNB->CNB_XPRZ)
					RecLock("CNB",.F.)
					CNB->CNB_XPRZ := 	nPrazo
					MsUnlock()
				EndIf
				(cAliasTmp)->(dbSkip())
			EndDo

			(cAliasTmp)->(dbCloseArea())
		EndIf
	EndIf
Next

RestArea(aArea)
Return


